<?php
/**
 * +----------------------------------------------------------------------
 * | Name: keAdmin
 * | Author King east To 1207877378@qq.com
 * +----------------------------------------------------------------------
 */


namespace app\common\traits;


use think\response\Json;

trait Jump
{

    /**
     * 返回数据
     * @param $httpCode
     * @param array $data
     * @return Json
     */
    protected function result($httpCode, array $data)
    {
        return json(array_merge(
            $data,
            $this->vars ?? []
        ), $httpCode);
    }


    /**
     * 返回成功数据
     * @param array $data
     * @param int $code
     * @param string $message
     * @param int $httpCode
     * @return Json
     */
    protected function success($data = [], $code = 0, string $message = 'SUCCESS', $httpCode = 200)
    {
        return json(array_merge(
            ['code'=>$code, 'message'=>$message],
            $this->vars ?? [],
            $data
        ), $httpCode);
    }


    /**
     * 返回失败数据
     * @param string $message
     * @param int $code
     * @param int $httpCode
     * @param array $data
     * @return Json
     */
    protected function error(string $message, $code = 1, array $data = [], $httpCode = 200)
    {
        return json(array_merge(
            ['code'=>$code, 'message'=>$message],
            $this->vars ?? [],
            $data
        ), $httpCode);
    }

}
